package com.google.api.adwords.starterkit.Synchronizer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;

import com.google.api.adwords.starterkit.settings.StarterKitSettings;
import com.google.api.adwords.starterkit.utils.DumpExceptions;
import com.google.api.adwords.v201101.cm.CampaignTargetOperation;
import com.google.api.adwords.v201101.cm.LanguageTarget;
import com.google.api.adwords.v201101.cm.LanguageTargetList;
import com.google.api.adwords.v201101.cm.Operator;

/**
 * The Class LanguageSynchronizer is responsible to keep all LanguageTarget 
 * information from AdWords system synchronized with local DB.
 * 
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class LanguageTargetSynchronizer extends TargetsSynchronizer {
  
  public LanguageTargetSynchronizer(StarterKitSettings starterKitSetting, 
      Connection conn) {
    super(starterKitSetting, conn);  
  }

  /**
   * Save the updates in languageTarget table to AdWords.
   */
  public void put() {
    log.debug("Preparing LanguageTarget to be sent to AdWords");
    languageTargetTable.updateCampaignsIds();
  
    ResultSet rs = languageTargetTable.getModified();
    try {
      HashSet<CampaignTargetOperation> campaignTargetOperation 
          = new HashSet<CampaignTargetOperation>();
      HashSet<LanguageTarget> languageTarget = new HashSet<LanguageTarget>();
      long lastCampaignId = 0;
      long campaignId = 0;
      String language = "";
      while (rs.next()) {
        campaignId = rs.getLong("campaignId");
        language = rs.getString("language");
        if (lastCampaignId != campaignId) {
          if (languageTarget.size() > 0) {
            LanguageTargetList languageTargetList = new LanguageTargetList();
            languageTargetList.setCampaignId(lastCampaignId);
            languageTargetList.setTargets(languageTarget
                .toArray(new LanguageTarget[languageTarget.size()]));
  
            CampaignTargetOperation languageTargetOperation 
                = new CampaignTargetOperation();
            languageTargetOperation.setOperand(languageTargetList);
            languageTargetOperation.setOperator(Operator.SET);
            campaignTargetOperation.add(languageTargetOperation);
          }
          lastCampaignId = campaignId;
          languageTarget.clear();
        }
        languageTarget.add(new LanguageTarget(null, language));
      }
      rs.close();  
      if (languageTarget.size() > 0) {
        LanguageTargetList languageTargetList = new LanguageTargetList();
        languageTargetList.setCampaignId(lastCampaignId);
        languageTargetList.setTargets(languageTarget
            .toArray(new LanguageTarget[languageTarget.size()]));
  
        CampaignTargetOperation languageTargetOperation 
            = new CampaignTargetOperation();
        languageTargetOperation.setOperand(languageTargetList);
        languageTargetOperation.setOperator(Operator.SET);
        campaignTargetOperation.add(languageTargetOperation);
      }
      processOperations(campaignTargetOperation);
    } catch (SQLException e) {
      DumpExceptions.dumpSQLException(e);
    }
    campaignTable.resetLanguageTargetFlags();
  }
}
